package com.arch.dev.client.view;

import java.util.Date;
import java.util.Set;

import com.arch.dev.client.BudgetTable;
import com.arch.dev.client.controller.BudgetController;
import com.arch.dev.client.view.DateSearchView.Types;
import com.arch.dev.client.view.ui.BudgetImageBundle;
import com.arch.dev.client.view.ui.ImageButton;
import com.arch.dev.client.vo.BudgetVO;
import com.arch.dev.client.vo.ExcelVO;
import com.arch.dev.client.vo.PdfVO;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.MultiSelectionModel;

public class EditBudgetView extends Composite{	
	
	@UiField
	public FilterDataView filterDataView;	
	
	@UiField
	public BudgetTable budgetTable;

	public DateSearchView dateSearchView;


	public DateSearchView getDateSearchView() {
		return dateSearchView;
	}
	
	private static EditBudgetViewUiBinder uiBinder = GWT
			.create(EditBudgetViewUiBinder.class);

	interface EditBudgetViewUiBinder extends UiBinder<Widget, EditBudgetView> {
	}

	public EditBudgetView() {	
		
		initWidget(uiBinder.createAndBindUi(this));
	
		BudgetImageBundle bundle = (BudgetImageBundle) GWT

				.create(BudgetImageBundle.class);

		ImageButton deleteImageButton  = new ImageButton(null, bundle.getDeleteIcon());
		
		

		deleteImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				MultiSelectionModel<BudgetVO> multiSelectionModel = budgetTable
						.getMultiSelectionModel();
				Set<BudgetVO> budgetVOs = multiSelectionModel.getSelectedSet();
				multiSelectionModel.clear();

				BudgetController.getInstance().deleteValues(EditBudgetView.this,
						budgetVOs);

			}
		});
		
		deleteImageButton.setTitle("Delete Records");
		
		budgetTable.addToolBarWidget(deleteImageButton);
		
		ImageButton editImageButton  = new ImageButton(null, bundle.getModifyIcon());
		
		final EditBudgetDialogView editBudgetDialogView = new EditBudgetDialogView("Cancel", this);
		editImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				MultiSelectionModel<BudgetVO> multiSelectionModel = budgetTable
						.getMultiSelectionModel();
				Set<BudgetVO> budgetVOs = multiSelectionModel.getSelectedSet();
				
				multiSelectionModel.clear();
				if(budgetVOs.size() == 1){
					editBudgetDialogView.setBudgetVO(budgetVOs.iterator().next());
					editBudgetDialogView.populateCatagoryListBox();
					editBudgetDialogView.show();
					//editBudgetDialogView.populateFields(budgetVOs.iterator().next());
				}else{
					Window.alert("Please select only one record");
				}
				

			}
		});
		
		editImageButton.setTitle("Edit Records");
		
		budgetTable.addToolBarWidget(editImageButton);
		
		ImageButton generatePDFImageButton  = new ImageButton(null, bundle.getPdfIcon());
		generatePDFImageButton.setTooltip("Download PDF");

		generatePDFImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {			
				BudgetController.getInstance().generateFilterPDF(budgetTable.getBudgetVOs(), EditBudgetView.this);

			}
		});
		
		generatePDFImageButton.setTitle("Download PDF");
		
		budgetTable.addToolBarWidget(generatePDFImageButton);
		
		ImageButton generateExcelImageButton  = new ImageButton(null, bundle.getExcelIcon());
		generateExcelImageButton.setTooltip("Download Excel");

		generateExcelImageButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {			
				BudgetController.getInstance().generateExcel(EditBudgetView.this, budgetTable.getBudgetVOs());
			}
		});
		
		generateExcelImageButton.setTitle("Download Excel");
		
		budgetTable.addToolBarWidget(generateExcelImageButton);
		
		dateSearchView = new DateSearchView(Types.ALL, this);
		
	/*	dateSearchView.addSearchButtonHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				BudgetController.getInstance().getDataForDate(dateSearchView.getDateBox().getValue(), EditBudgetView.this);
				
			}
		});*/
		
		budgetTable.addToolBarWidget(dateSearchView);
		
		filterDataView.addAllRadioButtonValueChangeHandler(new ValueChangeHandler<Boolean>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> arg0) {
				if(arg0.getValue().booleanValue() == true){
					dateSearchView.processComponents(Types.ALL);
					dateSearchView.addSearchButtonHandler(new ClickHandler() {						
						@Override
						public void onClick(ClickEvent event) {
							BudgetController.getInstance().getAllValues(EditBudgetView.this);
							
						}
					});
				}				
			}
		});
		
		filterDataView.addYearRadioButtonValueChangeHandler(new ValueChangeHandler<Boolean>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> arg0) {
				if(arg0.getValue().booleanValue() == true){
					dateSearchView.processComponents(Types.YEAR);
				/*	dateSearchView.addSearchButtonHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							BudgetController.getInstance().getDataForDate(dateSearchView.getDateBox().getValue(), EditBudgetView.this);
							
						}
					});*/
				}				
			}
		});
		
		
		filterDataView.addDateRadioButtonValueChangeHandler(new ValueChangeHandler<Boolean>() {
					
					@Override
					public void onValueChange(ValueChangeEvent<Boolean> arg0) {
						if(arg0.getValue().booleanValue() == true){
							dateSearchView.processComponents(Types.DATE);
							dateSearchView.addSearchButtonHandler(new ClickHandler() {
								
								@Override
								public void onClick(ClickEvent event) {
									BudgetController.getInstance().getDataForDate(new Date(dateSearchView.getDateBox().getValue()), EditBudgetView.this);
									
								}
							});
						}				
					}
				});

		filterDataView.addMonthRadioButtonValueChangeHandler(new ValueChangeHandler<Boolean>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<Boolean> arg0) {
				if(arg0.getValue().booleanValue() == true){
					dateSearchView.processComponents(Types.MONTH);
					/*dateSearchView.addSearchButtonHandler(new ClickHandler() {
						
						@Override
						public void onClick(ClickEvent event) {
							BudgetController.getInstance().getDataForDate(dateSearchView.getDateBox().getValue(), EditBudgetView.this);
							
						}
					});*/
				}				
			}
		});
	}

	
	public void getPDFFromServer(PdfVO pdfVO){
		FormPanel formPanel = new FormPanel();
		
		TextBox tb = new TextBox();
		tb.setName("fileName");
		tb.setText(pdfVO.getName());
		VerticalPanel vp = new VerticalPanel();
		vp.add(tb);
		formPanel.setWidget(vp);
		formPanel.setAction(pdfVO.getUrl());
		
		formPanel.setMethod(FormPanel.METHOD_GET);		
		formPanel.submit();
	}
	
	public void getExcelFromServer(ExcelVO excelVO){
		FormPanel formPanel = new FormPanel();		
		TextBox tb = new TextBox();
		tb.setName("fileName");
		tb.setText(excelVO.getName());
		VerticalPanel vp = new VerticalPanel();
		vp.add(tb);
		formPanel.setWidget(vp);
		formPanel.setAction(excelVO.getUrl());
		
		formPanel.setMethod(FormPanel.METHOD_GET);		
		formPanel.submit();
	}
	
	
	

}
